package others.swing.p011;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

/*学生成绩管理系统 版本T1.0学生操作界面*/
public class SDo extends JFrame implements ActionListener {
    Label t1, t2, sg1, sg2, sg3;
    JButton b1, b2, b3;
    String ad = "", y = "", sql = "";    //ad表示即将要访问的数据库名称（即访问者的身份），y表示访问者的用户名,sql数据库语句
    DBAccess db = new DBAccess("SMSystem", "", "");
    Student s;

    public SDo(String u, String x) {
        ad = u;
        y = x;
        s = new Student();
        t1 = new Label("欢迎您! " + y);
        t2 = new Label("您的成绩如下：");
        sg1 = new Label("语文：");
        sg2 = new Label("英语：");
        sg3 = new Label("数学：");

        b1 = new JButton("查询成绩");
        b2 = new JButton("修改密码");
        b3 = new JButton("注销");
        setLayout(null);

        //设置标签大小和所在位置
        t1.setBounds(40, 30, 180, 50);
        b1.setBounds(400, 80, 100, 50);
        b2.setBounds(400, 135, 100, 50);
        b3.setBounds(550, 350, 80, 50);

        t2.setBounds(40, 150, 300, 50);
        sg1.setBounds(50, 200, 105, 50);
        sg2.setBounds(50, 250, 105, 50);
        sg3.setBounds(50, 300, 105, 50);
        //设置标签内的字体
        t1.setFont(new Font("楷体", 1, 20));//字体大小
        t2.setFont(new Font("楷体", 1, 20));//字体大小
        sg1.setFont(new Font("楷体", 1, 20));//字体大小
        sg2.setFont(new Font("楷体", 1, 20));//字体大小
        sg3.setFont(new Font("楷体", 1, 20));//字体大小

        b1.setFont(new Font("楷体", 1, 15));//字体大小
        b2.setFont(new Font("楷体", 1, 15));//字体大小
        b3.setFont(new Font("楷体", 1, 15));//字体大小

        t1.setForeground(Color.BLUE);//字体颜色
        t2.setForeground(Color.BLUE);//字体颜色
        b1.setForeground(Color.BLUE);//字体颜色
        b2.setForeground(Color.BLUE);//字体颜色
        b3.setForeground(Color.BLUE);//字体颜色
        add(t1);
        add(t2);
        add(sg1);
        add(sg2);
        add(sg3);
        add(b1);
        add(b2);
        add(b3);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        setSize(700, 450);
        setVisible(true);
    }

    //实例方法：得到该学生的全部信息
    public int getStudent() {
        String id[], n[], pwd[], sc[];
        double g1[], g2[], g3[];
        int i = 0, j = 0, a = 0;        //a=0时是未设置学生信息或者没设置成功
        try {
            db.dbcon();
            sql = "SELECT * FROM " + ad;
            db.dbSelect(sql);
            while (db.rs.next()) {
                j++;
            }
            id = new String[j];
            n = new String[j];
            pwd = new String[j];
            sc = new String[j];
            g1 = new double[j];
            g2 = new double[j];
            g3 = new double[j];

            db.rs.beforeFirst();   //游标返回开头位置
            for (i = 0; i < j; i++) {
                db.rs.next();
                id[i] = db.rs.getString(2);
                n[i] = db.rs.getString(3);
                pwd[i] = db.rs.getString(4);
                sc[i] = db.rs.getString(5);
                g1[i] = db.rs.getDouble(9);
                g2[i] = db.rs.getDouble(10);
                g3[i] = db.rs.getDouble(11);
            }
            for (i = 0; i < j; i++) {
                if (y.equals(id[i])) {
                    s.setSName(n[i]);
                    s.setSID(id[i]);
                    s.setSPassword(pwd[i]);
                    s.setSClass(sc[i]);
                    s.setSGrade(g1[i], g2[i], g3[i]);//设置成绩
                    s.getSName();
                    s.getSID();
                    s.getSPassword();
                    s.getSClass();
                    s.getSGrade_1();
                    s.getSGrade_2();
                    s.getSGrade_3();
                    a = 1;    //设置成功
                }
            }
            db.dbclose();
        } catch (SQLException sqle) {
            System.out.println(1 + sqle.toString());
        } catch (Exception e) {
            System.out.println(2 + e.toString());
        }
        return a;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        getStudent();
        if (e.getSource() == b1) {
            if (getStudent() == 1) {
                t2.setText("您的成绩如下：");     //防止第二次点击按钮时字符出现重复
                t2.setText(s.SClass + "的同学，" + t2.getText());
                sg1.setText(sg1.getText() + s.SGrade[0]);
                sg2.setText(sg2.getText() + s.SGrade[1]);
                sg3.setText(sg3.getText() + s.SGrade[2]);
            } else {
                JOptionPane.showMessageDialog(null, "读取数据库出错！", "友情提示", 1);
            }
        } else if (e.getSource() == b2) {
            new Cpwd(ad, s.SID);
        } else if (e.getSource() == b3) {
            dispose();//setVisible(false);//清除界面
            new Login();
        }
    }
}